<template>
  <div class="alarm-map-wrap"></div>
</template>

<script>
import AMap from 'AMap'
import Alarm from '../../../../src/assets/scripts/plugins/alarm'
import config from '../../../config'

let alarmMap
export default {
  name: 'alarm',
  props: ['mapList', 'curMarker'],
  data () {
    return {
      mapStyle: config.mapStyle
    }
  },
  mounted () {
    let _this = this

    alarmMap = new Alarm({
      target: '.alarm-map-wrap',
      w: '100%',
      h: '100%',
      mapStyle: _this.mapStyle, // 设置地图的显示样式
      AMap: AMap,
      popWindowTemp: _this.popWindowTemp,
      uniqueId: 'eventSysId',
      data: []
    })
    alarmMap.init()
    this.$store.commit('setSupMap', alarmMap)
  },
  watch: {
    'mapList': function (res) {
      if (!res || res.length === 0) {
        alarmMap.clearDots()
        return
      }
      for (let i = 0, len = res.length; i < len; i++) {
        if (i === 0) {
          alarmMap.addDot(res[i], true)
        } else {
          alarmMap.addDot(res[i])
        }
      }
    }
  },
  methods: {
    popWindowTemp (data) {
      console.log(data.equipmentType)
      // 地图嵌入的信息窗文字
      let sContent = ''
      if (!data.equipmentType) {
        sContent += '<div class="mapStyle">'
        sContent += '<h2>报警详情</h2>'
        sContent += '<ul>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">编号:</div><div class="mapStyle-right">' + data.eventId + '</div></li>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">场景:</div><div class="mapStyle-right">' + data.moduleName + '</div></li>'

        if (data.eventValue) {
          sContent += '<li class="clearfix"><div class="mapStyle-left">' + data.eventTypeName + ':</div><div class="mapStyle-right">' + data.eventValue + '</div></li>'
        }
        sContent += '<li class="clearfix"><div class="mapStyle-left">报警时间:</div><div class="mapStyle-right">' + data.eventTime + '</div></li>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">地址:</div><div class="mapStyle-right">' + data.eventContent + '</div></li>'
        sContent += '<li class="clearfix operat"><div data-eventSysId="' + data.eventSysId + '" data-equipmentId="' + data.equipmentId + '" class="alarm-dispose"><button>处理</button></div><div data-eventSysId="' + data.eventSysId + '" data-equipmentId="' + data.equipmentId + '" class="alarm-misinfo"><button>误报</button></div></li>'
        sContent += '</ul>'
        sContent += '</div>'
      } else {
        let equipmentState = data.equipmentState === '1' ? '开启' : '关闭'
        let sContent = ''
        sContent += '<div class="mapStyle">'
        sContent += '<h2>设备详情</h2>'
        sContent += '<ul>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">编号:</div><div class="mapStyle-right">' + data.equipmentId + '</div></li>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">场景:</div><div class="mapStyle-right">' + data.moduleName + '</div></li>'

        if (data.eventValue) {
          sContent += '<li class="clearfix"><div class="mapStyle-left">' + data.eventTypeName + ':</div><div class="mapStyle-right">' + data.eventValue + '</div></li>'
        }
        sContent += '<li class="clearfix"><div class="mapStyle-left">状态:</div><div class="mapStyle-right">' + equipmentState + '</div></li>'
        sContent += '<li class="clearfix"><div class="mapStyle-left">地址:</div><div class="mapStyle-right">' + data.equipmentAddr + '</div></li>'
        sContent += '</ul>'
        sContent += '</div>'
        return sContent
      }
      return sContent
    }
  }
}
</script>

<style lang="less" scoped>
.alarm-map-wrap {
  width: 100%;
  height: 100%;
  /deep/ .amap-container {
    border-radius: 0 0 4px 4px;
  }
  /deep/ .mapStyle h2 {
    font-size: 14px;
    color: #f90;
    text-align: center;
  }
  /deep/ .mapStyle li {
    list-style: none;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 12px;
    line-height: 30px;
    padding: 2px 0;
  }
  /deep/ .operat {
    display: flex;
    justify-content: center;
    button {
      width: 60px;
      height: 30px;
      border-radius: 5px;
      border: none;
      outline: none;
      font-size: 16px;
      color: #fff;
      &:hover {
        cursor: pointer;
      }
    }
    .alarm-dispose {
      margin-right: 50px;
      button {
        background: #008be8;
      }
    }
    .alarm-misinfo {
      button {
        background: chocolate;
      }
    }
  }
  /deep/ .mapStyle-left {
    float: left;
    text-align: left;
    color: #9facc3;
  }
  /deep/ .mapStyle-right {
    float: right;
    text-align: right;
    color: #fff;
    width: 180px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  /deep/ .mapStyle-right .high {
    color: #ff6600;
  }
  /deep/ .myStyle-img {
    float: left;
    margin-left: 5px;
    width: 56px;
    height: 42px;
    overflow: hidden;
  }
  /deep/ .myStyle-img img {
    width: 56px;
  }
  /deep/ .mapStyle a {
    display: block;
    width: 245px;
    height: 40px;
    line-height: 40px;
    background-color: #ff6600;
    margin: 0 auto;
    text-align: center;
    color: #fff;
    text-decoration: none;
    cursor: pointer;
  }
  /deep/ .mapStyle h2 {
    margin: 0;
  }
}
</style>
